package com.felix.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;

import com.felix.bean.Message;
import com.felix.dao.InsertDao;
import com.felix.dao.impl.InsertDaoImpl;
import com.g3net.tool.Base64.OutputStream;

public class MakeFile {
	public static InsertDao iDao =new InsertDaoImpl();

	public static int saveConent(String pathname, String context) {
		int i=0;
		BufferedWriter writer = null;
		
		System.out.println(pathname);
		try {
		File file = new File(pathname);
		 if (!file.exists()) {//新会话
			 i=-1;
		 }
			OutputStreamWriter osw=new OutputStreamWriter(new FileOutputStream(file,true), "gbk");
			writer = new BufferedWriter(osw);
			writer.write(context + "\r\n");
			writer.flush();
			writer.close();

		} catch (IOException e) {
			e.printStackTrace();
		}
		return i;
	}

	public static int getLastKey(String pathname) {
		int pmkey = 0;
		File file = new File(pathname);
		if (!file.exists()) {
			return pmkey;
		}
		try {
			String lastLine = readLastLine(file, "utf-8");
			if (lastLine == null || "".equals(lastLine)) {
				return pmkey;
			}
			int len = lastLine.indexOf("##");

			pmkey = Integer.parseInt(lastLine.substring(0, len));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return pmkey;
	}

	public static List<Message> getMsg(String pathname, int mkey) {
		List<Message> msgs=new ArrayList<Message>();
		Message message=null;
		File file = new File(pathname);
		if (!file.exists()) {
			return null;
		}
		BufferedReader reader = null;
		try {
//			System.out.println("以行为单位读取文件内容，一次读一整行：");
			InputStreamReader isr = new InputStreamReader(new FileInputStream(file), "gbk");

			reader = new BufferedReader(isr);
			String tempString = null;
			int line = 1;
			// 一次读入一行，直到读入null为文件结束
			while ((tempString = reader.readLine()) != null) {
				// 显示行号
				
				String[] lin=tempString.split("##");
				int temp=Integer.parseInt(lin[0]);
				if (temp<=mkey) {
					continue;
				}
				message=new Message();
				message.setMkey(temp);
				message.setId(lin[1]);
				message.setName(lin[2]);
				message.setDateTime(lin[3]);
				if (lin.length<5) {
					message.setContent("");
					message.setStatus(0);
				}else if(lin[4].indexOf("||Auto")==-1){
					message.setContent(lin[4]);
					message.setStatus(0);
				}else {
					String[] sublin=lin[4].split("\\|\\|");
					message.setContent(sublin[0]);
					message.setStatus(1);
					message.setLinks(sublin[1]);
				}
				
				msgs.add(message);
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}

		return msgs;
	}

	public static String readLastLine(File file, String charset)
			throws IOException {
		if (!file.exists() || file.isDirectory() || !file.canRead()) {
			return null;
		}
		RandomAccessFile raf = null;
		try {
			raf = new RandomAccessFile(file, "r");
			long len = raf.length();
			if (len == 0L) {
				return ""; // 文件为空
			} else {
				long pos = len - 1;
				while (pos > 0) {
					pos--;
					raf.seek(pos);
					if (raf.readByte() == '\n') {
						break;
					}
				}
				if (pos == 0) {
					raf.seek(0);
				}
				byte[] bytes = new byte[(int) (len - pos)];
				raf.read(bytes);
				if (charset == null) {
					return new String(bytes);
				} else {
					return new String(bytes, charset);
				}
			}
		} catch (FileNotFoundException e) {
		} finally {
			if (raf != null) {
				try {
					raf.close();
				} catch (Exception e2) {
				}
			}
		}
		return null;
	}

	public static void main(String[] args) {
//		int a = getLastKey("C:/apache-tomcat-6.0.35/webapps/shun2/WEB-INF/classes/Record/324_34_23");
//		getMsg("C:/apache-tomcat-6.0.35/webapps/shun2/WEB-INF/classes/Record/324_34_23", 1234);
//		System.out.println(a);
		File file =new File("D:/Workspaces/MyEclipse/.metadata/.me_tcat/webapps/shun2/WEB-INF/classes/Record/10/324_20");
		System.out.println(file.exists());
	}

}
